package com.hzhk.happyrent.ui.activity.bill;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.bill.ProductBillBean;
import com.hzhk.happyrent.bean.person.ServiceBean;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.activity.PayPeriodsActivity;
import com.hzhk.happyrent.ui.adapter.bill.ProductBillAdapter;
import com.hzhk.happyrent.utils.MyPerference;
import com.tamic.novate.Throwable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BillListActivity extends AppCompatActivity {

    @BindView(R.id.activity_titlebar_center)
    TextView activityTitlebarCenter;
    @BindView(R.id.rent_count)
    TextView rentCount;
    @BindView(R.id.lv_bill)
    ListView lvBill;
    ProductBillAdapter productBillAdapter;
    @BindView(R.id.pay_total)
    TextView payTotal;
    @BindView(R.id.bill_notice)
    TextView billNotice;
    private String periods_id;
    private String order_sn;
    private String money;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bill_list);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        activityTitlebarCenter.setText("分期账单");
        productBillAdapter = new ProductBillAdapter(this);
        lvBill.setAdapter(productBillAdapter);
        initData();
        getServiceCode();
    }

    private void initData() {
        Map<String, Object> params = new HashMap<>();
        params.put("user_id", MyPerference.getInstance().getUserId());
        params.put("order_id", getIntent().getStringExtra("order_id"));
        HttpManager.getInstance().get(API.GET_ORDER_PERIODS, params, new MyStringResponse(this) {
            @Override
            public void onError(Object tag, Throwable e) {

            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }

            @Override
            public void onNext(Object tag, String response) {
                ProductBillBean productBillBean = new Gson().fromJson(response, ProductBillBean.class);
                if (0 == productBillBean.getErrCode()) {
                    List<ProductBillBean.DataBean.OrdersBean> orders = productBillBean.getData().getOrders();
                    int n = orders.size();
                    for (int i = 0; i < n; ) {
                        if ("1".equals(orders.get(i).getStatus())) {
                            orders.add(orders.get(i));
                            orders.remove(i);
                            n -= 1;
                        } else {
                            i++;
                        }
                    }
                    orders.get(0).setStatus("2");
                    periods_id = orders.get(0).getId();
                    order_sn = orders.get(0).getOrderSn();
                    money = orders.get(0).getMoney();
                    if (!TextUtils.isEmpty(orders.get(0).getExpirePayMoney())) {
                        money = (Double.parseDouble(money) + Double.parseDouble(orders.get(0).getExpirePayMoney())) + "";
                    }
                    payTotal.setText("￥" + money);
                    productBillAdapter.setList(orders);
                }
            }
        });

    }

    private void getServiceCode() {
        HttpManager.getInstance().get(API.APP_CONFIG, new HashMap<String, Object>(), new MyStringResponse(this) {
            @Override
            public void onError(Object tag, Throwable e) {

            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }

            @Override
            public void onNext(Object tag, String response) {
                ServiceBean serviceBean = new Gson().fromJson(response, ServiceBean.class);
                if (serviceBean != null && serviceBean.getErrCode() == 0) {
                    if (!TextUtils.isEmpty(serviceBean.getData().getTimeawayTip())) {
                        billNotice.setText(serviceBean.getData().getTimeawayTip());
                    }
                }
            }
        });
    }

    @OnClick({R.id.activity_titlebar_left, R.id.pay_choose})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_titlebar_left:
                finish();
                break;
            case R.id.pay_choose:
                Intent intent = new Intent(this, PayPeriodsActivity.class);
                intent.putExtra("periods_id", periods_id);
                intent.putExtra("order_sn", order_sn);
                intent.putExtra("pay_total", money);
                startActivity(intent);
                break;
        }
    }
}
